package com.yl.project.system.info.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yl.common.utils.file.FileUploadUtils;
import com.yl.common.utils.poi.ExcelUtil;
import com.yl.framework.aspectj.lang.annotation.Log;
import com.yl.framework.aspectj.lang.enums.BusinessType;
import com.yl.framework.config.YlConfig;
import com.yl.framework.web.controller.BaseController;
import com.yl.framework.web.domain.AjaxResult;
import com.yl.framework.web.page.TableDataInfo;
import com.yl.project.system.info.domain.FileInfo;
import com.yl.project.system.info.service.IFileInfoService;

/**
 * 文件信息Controller
 * 
 * @author ruoyi
 * @date 2020-04-27
 */
@Controller
@RequestMapping("/system/info")
public class FileInfoController extends BaseController
{
    private String prefix = "system/info";

    @Autowired
    private IFileInfoService fileInfoService;
    
private final ResourceLoader resourceLoader;
	
	@Autowired  
	public FileInfoController(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader; 
	}

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    /**
     * 查询文件信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FileInfo fileInfo)
    {
        startPage();
        List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FileInfo fileInfo)
    {
        List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
        ExcelUtil<FileInfo> util = new ExcelUtil<FileInfo>(FileInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增文件信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FileInfo fileInfo)
    {
        return toAjax(fileInfoService.insertFileInfo(fileInfo));
    }

    /**
     * 修改文件信息
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        FileInfo fileInfo = fileInfoService.selectFileInfoById(fileId);
        mmap.put("fileInfo", fileInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件信息
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FileInfo fileInfo)
    {
        return toAjax(fileInfoService.updateFileInfo(fileInfo));
    }

    /**
     * 删除文件信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fileInfoService.deleteFileInfoByIds(ids));
    }
    
    @PostMapping("/fileUpload")
    @ResponseBody
    public AjaxResult fileUpload(@RequestParam("file") MultipartFile file, FileInfo fileInfo) throws IOException
    {
    	// 上传文件路径
    	String filePath = YlConfig.getUploadPath();
    	// 上传并返回新文件名称
    	String fileName = FileUploadUtils.upload(filePath, file);
    	fileInfo.setFilePath(fileName);
    	return toAjax(fileInfoService.insertFileInfo(fileInfo));
    }
    
    @RequestMapping("/showUrl")
	  @ResponseBody
	  public ResponseEntity<?> showUrl(String url){
	    try {
	      // 由于是读取本机的文件，file是一定要加上的，
	      return ResponseEntity.ok(resourceLoader.getResource("file:"+ YlConfig.getProfile()+File.separator+url));
	    } catch (Exception e) {
	      return ResponseEntity.notFound().build();
	    }
	  }
}
